package DAO;

import javabean.Technician;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TechnicianDAO implements ITechnicianDAO {
    @Override
    public boolean login(Technician technician){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean success = false;
        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Technician WHERE TechnicianID = ? AND Password = ?");
            statement.setString(1, technician.getTechnicianID());
            statement.setString(2, technician.getPassword());
            resultSet = statement.executeQuery();
            success = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }
        return success;
    }

    @Override
    public List<Technician> getAllTechnicians() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        List<Technician> technicianList = new ArrayList<>();

        try {
            connection = ConnectManager.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM Technician");

            while (resultSet.next()) {
                Technician technician = new Technician();
                technician.setTechnicianID(resultSet.getString("TechnicianID"));
                technician.setTechnicianName(resultSet.getString("TechnicianName"));
                technician.setTechnicianPhone(resultSet.getString("TechnicianPhone"));
                technician.setPassword(resultSet.getString("Password"));
                technician.setAdminID(resultSet.getString("AdminID"));

                technicianList.add(technician);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return technicianList;
    }

    @Override
    public Technician getTechnicianByID(String technicianID) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Technician technician = null;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Technician WHERE TechnicianID = ?");
            statement.setString(1, technicianID);

            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                technician = new Technician();
                technician.setTechnicianID(resultSet.getString("TechnicianID"));
                technician.setTechnicianName(resultSet.getString("TechnicianName"));
                technician.setTechnicianPhone(resultSet.getString("TechnicianPhone"));
                technician.setPassword(resultSet.getString("Password"));
                technician.setAdminID(resultSet.getString("AdminID"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return technician;
    }

    @Override
    public boolean addPendingTechnician(Technician technician) {
        // TODO 未考虑到重复添加的情况，与已注册的技术人员ID重复
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            //判断TechnicianID是否已经存在
            statement = connection.prepareStatement("SELECT * FROM Technician WHERE TechnicianID = ?");
            statement.setString(1, technician.getTechnicianID());
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return false;
            }
            statement = connection.prepareStatement("INSERT INTO PendingTechnician (TechnicianID, TechnicianName, TechnicianPhone, Password) VALUES (?, ?, ?, ?)");
            statement.setString(1, technician.getTechnicianID());
            statement.setString(2, technician.getTechnicianName());
            statement.setString(3, technician.getTechnicianPhone());
            statement.setString(4, technician.getPassword());

            int rowsInserted = statement.executeUpdate();
            if (rowsInserted > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }

    @Override
    public boolean updateTechnician(Technician technician) {
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("UPDATE Technician SET TechnicianName = ?, TechnicianPhone = ?, Password = ?, AdminID = ? WHERE TechnicianID = ?");
            statement.setString(1, technician.getTechnicianName());
            statement.setString(2, technician.getTechnicianPhone());
            statement.setString(3, technician.getPassword());
            statement.setString(4, technician.getAdminID());
            statement.setString(5, technician.getTechnicianID());

            int rowsUpdated = statement.executeUpdate();
            if (rowsUpdated > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }

    @Override
    public boolean deleteTechnician(String technicianID) {
        Connection connection = null;
        PreparedStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("DELETE FROM Technician WHERE TechnicianID = ?");
            statement.setString(1, technicianID);

            int rowsDeleted = statement.executeUpdate();
            if (rowsDeleted > 0) {
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }


    @Override
    public List<Technician> getAllPendingTechnicians() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        List<Technician> technicianList = new ArrayList<>();

        try {
            connection = ConnectManager.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM PendingTechnician");

            while (resultSet.next()) {
                Technician technician = new Technician();
                technician.setPendingID(resultSet.getInt("PendingID"));
                technician.setTechnicianID(resultSet.getString("TechnicianID"));
                technician.setTechnicianName(resultSet.getString("TechnicianName"));
                technician.setTechnicianPhone(resultSet.getString("TechnicianPhone"));
                technician.setPassword(resultSet.getString("Password"));

                technicianList.add(technician);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }

        return technicianList;
    }
}
